package com.yunxiao.cloud.web.task;

import com.yunxiao.cloud.mq.util.RabbitUtil;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: ConsumerListener
 * @author: yunxiao
 * @date: 2020/1/8 10:06
 * @version: V1.0
 **/
@Component
public class ConsumerListener {

    @Autowired
    private SimpleMessageListenerContainer messageListenerContainer;

    @Autowired
    private RabbitUtil rabbitUtil;

    @PostConstruct
    public void initQueues() {
        rabbitUtil.init("dispatch.order.topic.exchange","habao.route", "dispatch.order.topic.habao", "topic");
        rabbitUtil.init("dispatch.order.topic.exchange","yinxiao.route", "dispatch.order.topic.yinxiao", "topic");
    }

    @Bean
    public void receive() {
        messageListenerContainer.addQueueNames("TestWebMQName");
        messageListenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
        MessageListenerAdapter listenerAdapter = new MessageListenerAdapter(new MessageHandler());

        Map<String, String> queueOrTagToMethodName = new HashMap<>(16);
        queueOrTagToMethodName.put("TestWebMQName", "handleMessage");
        listenerAdapter.setQueueOrTagToMethodName(queueOrTagToMethodName);
        messageListenerContainer.setMessageListener(listenerAdapter);
    }

}
